package com.wh.hotel.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.common.annotation.ResultLog;
import com.wh.common.enums.MethodTypeEnum;
import com.wh.hotel.constants.CommonConstant;
import com.wh.hotel.mapper.HotelBookRuleMapper;
import com.wh.hotel.model.HotelBookRule;
import com.wh.hotel.vo.HotelBookRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wenhao
 */

@Slf4j
@Component
public class HotelBookRuleDao {

    @Resource
    private HotelBookRuleMapper hotelBookRuleMapper;

    @ResultLog(name = "HotelBookRuleDao.selectPage", methodType = MethodTypeEnum.DB, printResult = false)
    public IPage<HotelBookRule> selectPage(HotelBookRuleVo hotelBookRuleVo) {
        IPage<HotelBookRule> page = new Page<>(hotelBookRuleVo.getPageNum(), hotelBookRuleVo.getPageSize());
        LambdaQueryWrapper<HotelBookRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(hotelBookRuleVo.getId() != null, HotelBookRule::getId, hotelBookRuleVo.getId());
        queryWrapper.likeRight(StringUtils.isNotBlank(hotelBookRuleVo.getRuleName()), HotelBookRule::getRuleName, hotelBookRuleVo.getRuleName());
        queryWrapper.orderByDesc(HotelBookRule::getUpdateDt);
        return hotelBookRuleMapper.selectPage(page, queryWrapper);
    }

    @ResultLog(name = "HotelBookRuleDao.selectBatchIds", methodType = MethodTypeEnum.DB, printResult = false)
    public List<HotelBookRule> selectBatchIds(List<Long> ruleIdList) {
        return hotelBookRuleMapper.selectBatchIds(ruleIdList);
    }

    @ResultLog(name = "HotelBookRuleDao.selectById", methodType = MethodTypeEnum.DB, printResult = false)
    public HotelBookRule selectById(Long ruleId) {
        return hotelBookRuleMapper.selectById(ruleId);
    }

    @ResultLog(name = "HotelBookRuleDao.insert", methodType = MethodTypeEnum.DB)
    public boolean insert(HotelBookRule hotelBookRule) {
        return hotelBookRuleMapper.insert(hotelBookRule) >= CommonConstant.SUCCESS_DB;
    }

    @ResultLog(name = "HotelBookRuleDao.update", methodType = MethodTypeEnum.DB)
    public boolean update(HotelBookRule hotelBookRule) {
        return hotelBookRuleMapper.updateById(hotelBookRule) >= CommonConstant.SUCCESS_DB;

    }
}
